<template>
  <div>
    <el-form ref="myform" :model="formItem" label-width="120px" size="mini" class="form-view">
      <el-row :gutter="40">
        <el-col :span="12">
          <el-form-item label="日期">
            {{ formatDate(formItem.dailyReportDate,'YYYY-MM-DD') }}
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="单位">
            {{ formItem.unitName }}
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="运维小组">
            {{ formItem.operationGroupName }}
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="处理人">
            {{ formItem.incidentUserName }}
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="服务时间">
            {{ formatDate(formItem.serviceStartTime,'HH:mm') }} 至 {{ formatDate(formItem.serviceEndTime,'HH:mm') }}
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="工作总时长">
            {{ formItem.totalWorkingHours }}
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="工作内容">
            <div style="height: 100px;">
              {{ formItem.jobContent }}
            </div>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="请求人/处室">
            {{ formItem.eventRequester }}
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="处理地点">
            {{ formItem.incidentLocation }}
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="事件等级">
            {{ getName(eventgradeList,formItem.eventLevel) }}
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="响应方式">
            {{ getName(responseMethodList,formItem.responseMethod) }}
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="服务类型">
            {{ formItem.serviceTypeName }}
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="服务属性">
            {{ formItem.serviceAttributesName }}
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="服务单号">
            {{ formItem.trackingNo }}
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注">
            <div style="height: 100px;">
              {{ formItem.remarks }}
            </div>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="附件">
            <div v-for="fileObj in formItem.attachment" :key="fileObj.fsId">
              <el-link type="primary" :href="downloadFileUrl(fileObj.fsId)" :download="downloadFileUrl(fileObj.fsId)" target="_blank">{{ fileObj.name }}</el-link>
            </div>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>

// 定义一个表单原始项
const formItem = {
  /** 日报日期 */
  dailyReportDate: '',
  /** 单位编码 */
  unitCode: '',
  /** 单位名称 */
  unitName: '',
  /** 运维小组编码 */
  operationGroupCode: '',
  /** 运维小组名称 */
  operationGroupName: '',
  /** 事件请求人/处室 */
  eventRequester: '',
  /** 响应方式 */
  responseMethod: '',
  /** 事件处理地点 */
  incidentLocation: '',
  /** 事件处理人编码 */
  incidentUserCode: '',
  /** 事件处理人名称 */
  incidentUserName: '',
  /** 服务类型编码 */
  serviceTypeCode: '',
  /** 服务类型名称 */
  serviceTypeName: '',
  /** 处理日期 */
  createTime: '',
  /** 服务属性编码 */
  serviceAttributesCode: '',
  time: null,
  /** 服务属性名称 */
  serviceAttributesName: '',
  /** 服务开始时间 */
  serviceStartTime: '',
  /** 服务结束时间 */
  serviceEndTime: '',
  /** 事件等级 */
  eventLevel: '',
  /** 工作总时长 */
  totalWorkingHours: 0,
  /** 服务单号 */
  trackingNo: '',
  /** 工作内容 */
  jobContent: '',
  /** 备注 */
  remarks: '',
  /** 附件 */
  attachment: []
}

import { formatDate } from '@neu/biz-core/utils/utils'
import { detail, downloadFileUrl } from '@neu/dcom-ui/api/workDaily'
import { list as dList } from '@neu/dcom-ui/api/dictionary'
import { list as groupList } from '@neu/dcom-ui/api/operationGroup'
export default {
  name: 'WorkDailyEditForm',
  props: {
    // 根据id可判断是新建还是更新
    id: {
      required: false,
      type: Number,
      default: -1
    }
  },
  data() {
    return {
      // 编辑的表单内容
      formItem: {
        ...formItem
      },
      /** 行数 */
      rows: 2,
      // 所有单位
      availableInstitutions: [],
      // 运维小组
      groups: [],
      // 响应方式
      responseMethodList: [],
      // 事件等级集合
      eventgradeList: [],
      formatDate,
      downloadFileUrl: downloadFileUrl
    }
  },
  watch: {
    // 监控id的变化，初始化表单数据
    id: function() {
      this.fetch()
    }
  },
  created() {
    this.fetch()
    // 获取小组
    groupList().then(res => {
      this.groups = res.data
    })

    // 响应方式
    dList({ s_type: 'responseMethod' }).then(res => {
      this.responseMethodList = res.data
    })

    // 事件等级
    dList({ s_type: 'eventgrade' }).then(res => {
      this.eventgradeList = res.data
    })
  },
  methods: {
    // 初始化表单数据
    fetch() {
      if (this.id !== -1) {
        detail(this.id).then(response => {
          const data = response.data
          if (data.attachment) {
            const fileJoins = data.attachment.split('||')
            data.attachment = fileJoins.map(it => {
              const fsInfo = it.split('@@')
              return {
                fsId: fsInfo[0],
                name: fsInfo[1]
              }
            })
          } else {
            data.attachment = []
          }
          this.formItem = {
            ...data
          }
        })
      } else {
        this.formItem = {
          ...formItem
        }
      }
    },

    reset() {
      this.$refs.myform.resetFields()
    },
    // 获取字典表名称
    getName(list, code) {
      const one = list.find(e => e.code === code)
      return one ? one.text : ''
    }
  }
}
</script>
